package com.nut.spp.api.admin;

import com.nut.spp.api.req.LoginReq;
import com.nut.spp.api.resp.UserInfoResp;
import com.nut.spp.common.Holder;
import com.nut.spp.common.Result;
import com.nut.spp.interceptor.AdminJwt;
import com.nut.spp.service.AuthService;
import com.nut.spp.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/admin/auth")
public class AuthController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private AuthService authService;

    @GetMapping("test")
    public Result test() {
        return Result.ok();
    }

    @PostMapping("login")
    public Result login(@RequestBody LoginReq loginReq) throws Exception {
        return sysUserService.login(loginReq);
    }

    //获取验证码
    @GetMapping("imgCode")
    public Result<Object> imgCode() {
        return authService.imgCode();
    }

    @GetMapping(value = "info")
    public Result<UserInfoResp> getUserInfo() {
        AdminJwt adminJwt = Holder.ADMIN_JWT_THREAD_LOCAL.get();
        Integer userId = adminJwt.getUserId();
        String username = adminJwt.getUsername();
        return sysUserService.getUserInfo(userId, username);
    }

}
